package com.fly.uaa.exceptionhandler;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fly.common.common.ErrorConstant;
import com.fly.common.exception.BusinessException;

import java.io.IOException;
import java.util.Map;

public class CustomOauth2ExceptionSerializer extends StdSerializer<CustomOauth2Exception> {
    public CustomOauth2ExceptionSerializer() {
        super(CustomOauth2Exception.class);
    }

    @Override
    public void serialize(CustomOauth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        BusinessException businessException = getBusinessException(e.getOAuth2ErrorCode(), e.getMessage());
        jsonGenerator.writeNumberField("resultCode", businessException.getErrorCode());
        jsonGenerator.writeStringField("resultMessages", businessException.getMessage());
        if (e.getAdditionalInformation() != null) {
            for (Map.Entry<String, String> entry : e.getAdditionalInformation().entrySet()) {
                String key = entry.getKey();
                String add = entry.getValue();
                jsonGenerator.writeStringField(key, add);
            }
        }
        jsonGenerator.writeEndObject();
    }

    private BusinessException getBusinessException(String oAuth2ErrorCode, String message) {
        Integer errorCode;
        String errorMessage;

        switch (oAuth2ErrorCode) {
            case "invalid_client":
                errorCode = ErrorConstant.INVALID_CLIENT;
                //errorMessage = ErrorConstant.INVALID_CLIENT_MSG;
                errorMessage = message;
                break;
            case "invalid_grant":
                if (message == ErrorConstant.INVALID_GRANT_BAD_EN_MSG) {
                    errorCode = ErrorConstant.INVALID_GRANT_BAD;
                    errorMessage = ErrorConstant.INVALID_GRANT_BAD_CN_MSG;
                } else if (message == ErrorConstant.INVALID_GRANT_LOCKED_EN_MSG) {
                    errorCode = ErrorConstant.INVALID_GRANT_LOCKED;
                    errorMessage = ErrorConstant.INVALID_GRANT_LOCKED_CN_MSG;
                } else {
                    errorCode = ErrorConstant.INVALID_GRANT;
                    errorMessage = message;
                }
                break;
            case "invalid_request":
                errorCode = ErrorConstant.INVALID_REQUEST;
                errorMessage = ErrorConstant.INVALID_REQUEST_MSG;
                break;
            case "invalid_scope":
                errorCode = ErrorConstant.INVALID_SCOPE;
                errorMessage = ErrorConstant.INVALID_SCOPE_MSG;
                break;
            case "invalid_token":
                errorCode = ErrorConstant.INVALID_TOKEN;
                errorMessage = ErrorConstant.INVALID_TOKEN_MSG;
                break;
            case "unauthorized_user":
                errorCode = ErrorConstant.UNAUTHORIZED_USER;
                errorMessage = ErrorConstant.UNAUTHORIZED_MSG;
                break;
            case "unsupported_grant_type":
                errorCode = ErrorConstant.UNSUPPORTED_GRANT_TYPE;
                errorMessage = ErrorConstant.UNSUPPORTED_GRANT_TYPE_MSG;
                break;
            case "unsupported_response_type":
                errorCode = ErrorConstant.UNSUPPORTED_RESPONSE_TYPE;
                errorMessage = ErrorConstant.UNSUPPORTED_RESPONSE_TYPE_MSG;
                break;
            case "access_denied":
                errorCode = ErrorConstant.ACCESS_DENIED;
                errorMessage = ErrorConstant.ACCESS_DENIED_MSG;
                break;
            case "server_error":
                errorCode = ErrorConstant.METHOD_EXCEPTION;
                errorMessage = ErrorConstant.METHOD_EXCEPTION_MSG;
                break;
            case "unauthorized":
                errorCode = ErrorConstant.UNAUTHORIZED;
                errorMessage = ErrorConstant.UNAUTHORIZED_MSG;
                break;
            case "method_not_allowed":
                errorCode = ErrorConstant.METHOD_NOT_ALLOWED;
                errorMessage = ErrorConstant.METHOD_NOT_ALLOWED_MSG;
                break;
            case "unauthorized_client":
                errorCode = ErrorConstant.UNAUTHORIZED_CLIENT;
                errorMessage = ErrorConstant.UNAUTHORIZED_CLIENT_MSG;
                break;
            case "invalid_sign":
                errorCode = ErrorConstant.INVALID_SIGN;
                errorMessage = ErrorConstant.INVALID_SIGN_MSG;
                break;
            case "empty_client":
                errorCode = ErrorConstant.EMPTY_CLIENT;
                errorMessage = ErrorConstant.EMPTY_CLIENT_MSG;
                break;
            case "empty_sign":
                errorCode = ErrorConstant.EMPTY_SIGN;
                errorMessage = ErrorConstant.EMPTY_SIGN_MSG;
                break;
            case "username_password_error":
                errorCode = ErrorConstant.USERNAME_PASSWORD_ERROR;
                errorMessage = message;
                break;
            case "user_lock_error":
                errorCode = ErrorConstant.USER_LOCKED;
                errorMessage = ErrorConstant.USER_LOCKED_MSG;
                break;
            default:
                errorCode = ErrorConstant.METHOD_EXCEPTION;
                errorMessage = ErrorConstant.METHOD_EXCEPTION_MSG;
        }

        return new BusinessException(errorCode, errorMessage);
    }
}
